package com.nulstudio.sppm.Service;


import com.nulstudio.sppm.Dao.UserInfoMapper;
import com.nulstudio.sppm.Entities.UserInfo;
import com.nulstudio.sppm.Utils.EncodingUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserInfoService {
    @Autowired
    private UserInfoMapper userInfoMapper;
    public int addUserInfo(UserInfo userInfo){
        userInfo.setPassword(userInfo.getPassword());
        return userInfoMapper.addUserInfo(userInfo);
    }
    public boolean checkMatched(String account,String password){
        return userInfoMapper.checkMatched(account,password) == 1;
    }
    public boolean checkExisted(String account){
        return userInfoMapper.checkExisted(account) > 0;
    }
    public boolean checkRepeatedUName(String uName){
        return userInfoMapper.checkRepeatedUName(uName) > 0;
    }
    public UserInfo getInfoByAccount(String account){
        return userInfoMapper.getInfoByAccount(account);
    }

    public UserInfo getInfoByUid(String uid){
        return userInfoMapper.getInfoByUid(Integer.parseInt(uid));
    }

    public boolean checkExistedUid(String uid){
        return userInfoMapper.checkExistedUid(Integer.parseInt(uid)) > 0;
    }
    public String modifyName(String uid,String name){
        if(checkExistedUid(uid)) {
            if (!checkRepeatedUName(name)) {
                userInfoMapper.modifyName(uid, name);
                return "success";
            }
            return "repeated name";
        }
        return "nonexistent user";
    }
    public int modifyAuthority(String uid,String authority){
        return userInfoMapper.modifyAuthority(uid,authority);
    }
    public int modifyPassword(String uid,String password){
        return userInfoMapper.modifyPassword(uid,password);
    }

    public int deleteUser(String uid){return userInfoMapper.deleteUser(Integer.parseInt(uid));}

    public List<UserInfo> getAllUsers(){return userInfoMapper.getAllUsers();}
    public int modifyUserInfo(UserInfo userInfo){
        return userInfoMapper.modifyUserInfo(userInfo);
    }

    public int getLastId() {
        return userInfoMapper.getLastId();
    }
    public String getAuthorityById(int id){return userInfoMapper.getAuthorityById(id);}
}
